package com.lowang.zookeeper;

import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = DistributedZkClient.build("localhost:2181,localhost:2182,localhost:2183");
        zk.delete("/lock", 0);
        Stat stat = zk.exists("/lock", false);
        if (stat == null) {
            //没有目录,顶级目录必须是PERSISTENT
            String str = zk.create("/lock", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(str);
        }
        String str = zk.create("/lock/sub-", "lockWang".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(str);
        while (true) {
            Thread.sleep(5000);
            try {
                List<String> list = zk.getChildren("/lock", false);
                System.out.println(list);
                System.out.println(list.size());
                for (String path : list) {
                    byte[] bs = zk.getData("/lock/" + path, null, null);
                    System.out.println(new String(bs));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
